package com.sinothk.gpserver.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.gpserver.data.DataConstant;
import com.sinothk.gpserver.modules.biz.entity.RoomCardLogEntity;
import com.sinothk.gpserver.modules.biz.mapper.RoomCardLogMapper;
import com.sinothk.gpserver.modules.biz.service.IRoomCardLogService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.gpserver.modules.system.entity.UserEntity;
import com.sinothk.gpserver.modules.system.entity.UserRoleEntity;
import com.sinothk.gpserver.modules.system.mapper.LoginMapper;
import com.sinothk.gpserver.modules.system.mapper.UserMapper;
import com.sinothk.gpserver.modules.system.mapper.UserRoleMapper;
import com.sinothk.gpserver.util.PageDataUtil;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 好友邀请功能 服务实现类
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-09-13
 */
@Service
public class RoomCardLogServiceImpl extends ServiceImpl<RoomCardLogMapper, RoomCardLogEntity> implements IRoomCardLogService {
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoomCardLogMapper roomCardLogMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private LoginMapper loginMapper;

    /**
     * 查询分页数据
     *
     * @param pageVo 分页参数
     * @return PageData
     */
    @Override
    public PageData<RoomCardLogEntity> page(PageVo<RoomCardLogEntity> pageVo) {
        Page<RoomCardLogEntity> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());

        LambdaQueryWrapper<RoomCardLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomCardLogEntity::getSetAccount, pageVo.getData().getSetAccount());
        wrapper.orderByDesc(RoomCardLogEntity::getCreateTime);

        IPage<RoomCardLogEntity> iPage = roomCardLogMapper.selectPage(page, wrapper);
        return new PageDataUtil<RoomCardLogEntity>().create(iPage);
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public void addLog(String getAccount, String value) {
        LoginEntity currUser = (LoginEntity) SecurityUtils.getSubject().getPrincipal();

        // 验证
        LambdaQueryWrapper<UserRoleEntity> queryWrapperUserRole = new LambdaQueryWrapper<>();
        queryWrapperUserRole.eq(UserRoleEntity::getUserAccount, currUser.getAccount());
        List<UserRoleEntity> userRoles = userRoleMapper.selectList(queryWrapperUserRole);
        StringBuilder rolesStr = new StringBuilder();
        for (UserRoleEntity userRole : userRoles) {
            rolesStr.append(userRole.getRoles()).append(",");
        }
        if (!rolesStr.toString().contains(DataConstant.RoleSystem.ADMIN)) {
            throw new BizException("当前用户没有操作权限");
        }

        RoomCardLogEntity roomCardLog = new RoomCardLogEntity();
        roomCardLog.setSetAccount(currUser.getAccount());
        roomCardLog.setCreateBy(currUser.getUserName());

        // 得到人信息
        UserEntity getUser = userMapper.selectByAccount(getAccount);
        if (getUser == null) throw new BizException("发放对象不存在");
        roomCardLog.setGetAccount(getUser.getAccount());
        roomCardLog.setGetAvatar(getUser.getUserAvatar());
        roomCardLog.setGetName(getUser.getUserName());
        //
        roomCardLog.setType(0);
        roomCardLog.setSetInfo(currUser.getAccount() + "发给" + getAccount);
        int valueInt = 0;
        try {
            valueInt = Integer.parseInt(value);
            roomCardLog.setGetCards(valueInt);
        } catch (Exception e) {
            throw new BizException("输入房卡数可能有误");
        }
        this.save(roomCardLog);

        // 更新得到人信息
        int cards = getUser.getRoomCards() == null ? 0 : getUser.getRoomCards();
        cards = cards + valueInt;
        getUser.setRoomCards(cards);
        userMapper.updateById(getUser);
    }
}
